<!-- 轮播图 -->

<template>
    <div class="carousel-container">
        <el-carousel ref="carouselRef" trigger="click" :interval="intervalTime" :motion-blur="true" indicator-position="outside" height="400px">
            <el-carousel-item @click="openArticle(item.id)" v-for="item in list" :key="item">
                <div class="carousel-content">
                    <el-image class="carousel-image" :src="item.img" fit="cover" :alt="item.title" loading="eager">
                        <template #placeholder>
                            <div class="image-placeholder">
                                <el-icon class="is-loading"><Loading /></el-icon>
                            </div>
                        </template>
                    </el-image>
                    <div class="carousel-title">
                        <h3>{{ item.title }}</h3>
                        <!-- <div class="carousel-date">{{ item.view_date }}</div> -->
                    </div>
                </div>
            </el-carousel-item>
        </el-carousel>
    </div>
</template>

<script setup lang='ts'>
import { getHotCarousel } from '@/api/home2';
import { getimage } from '@/api/image';
import { onMounted, ref } from 'vue';
import { Loading } from '@element-plus/icons-vue';
import { ElCarousel } from 'element-plus';

const carouselRef = ref<InstanceType<typeof ElCarousel>>();
const intervalTime = 3000
// 自动切换时间间隔
const list = ref(
    [
    { id: 1, title: '照片1', img: '' },
    { id: 2, title: '照片2', img: '' },
    { id: 3, title: '照片3', img: '' },
    { id: 4, title: '照片4', img: '' },
    { id: 5, title: '照片5', img: '' },
    { id: 6, title: '照片6', img: '' },
    { id: 7, title: '照片7', img: '' },
]
);
const SynchotCarousel = async () => {
    const response = await getHotCarousel()
    list.value = response.data.data
    list.value.forEach((item: { img: string; }) => {
        item.img = getimage(item.img)
    });
    // console.log(list.value);

}
const openArticle = (id) => {
    window.location.href='/article/'+id
}
onMounted(() => {
    SynchotCarousel().then(() => {
        // 数据加载完成后，延迟500ms自动切换到下一张
        setTimeout(() => {
            carouselRef.value?.next()
        }, 500)
    })
})
</script>

<style>
.carousel-container {
    width: 100%;
    position: relative;
    border-radius: 8px;
    overflow: hidden;
}

.carousel-content {
    position: relative;
    width: 100%;
    height: 100%;
}

.carousel-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.carousel-title {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 20px;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
    color: white;
}

.carousel-title h3 {
    margin: 0;
    font-size: 24px;
    font-weight: bold;
}

.carousel-date {
    margin-top: 8px;
    font-size: 14px;
    opacity: 0.8;
}

:deep(.el-carousel__arrow) {
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    color: #333;
    width: 40px;
    height: 40px;
    transform: translateY(-50%);
}

:deep(.el-carousel__arrow:hover) {
    background-color: white;
}

:deep(.el-carousel__indicators) {
    bottom: 20px;
}

:deep(.el-carousel__indicator) {
    padding: 0 4px;
}

:deep(.el-carousel__button) {
    width: 30px;
    height: 3px;
    background-color: rgba(255, 255, 255, 0.7);
    border-radius: 3px;
}

.image-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f7fa;
}

.image-placeholder .el-icon {
    font-size: 24px;
    color: #909399;
}

:deep(.el-carousel__indicator.is-active .el-carousel__button) {
    background-color: white;
}
</style>